package com.gopay.common.cps.dao.trans.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cps.dao.trans.CpsReceiptSplitDtlQueryDAO;
import com.gopay.common.domain.cps.CpsReceiptSplitDtl;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author baiyunpeng
 */
@Repository
public class CpsReceiptSplitDtlQueryDAOImpl extends BaseReadDAOImpl<CpsReceiptSplitDtl,String> implements CpsReceiptSplitDtlQueryDAO {

    @Override
    public List<CpsReceiptSplitDtl> queryDtlsByGopayOrderId(String gopayOrderId){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CpsReceiptSplitDtl.class);
        detachedCriteria.add(Restrictions.eq("mainGopayOrderId", gopayOrderId));
        return findByCriteria(detachedCriteria);
    }

    @Override
    public CpsReceiptSplitDtl queryPlatformDtlsByGopayOrderId(String gopayOrderId){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CpsReceiptSplitDtl.class);
        detachedCriteria.add(Restrictions.eq("mainGopayOrderId", gopayOrderId));
        detachedCriteria.add(Restrictions.eq("isPlatform", "1"));

        List<CpsReceiptSplitDtl> cpsReceiptSplitDtlList = findByCriteria(detachedCriteria);
        if(!cpsReceiptSplitDtlList.isEmpty()){
            return cpsReceiptSplitDtlList.get(0);
        }
        return null;
    }

    @Override
    public CpsReceiptSplitDtl queryByMainOrderIdAndSplitAcct(String mainGopayOrderId,String splitAcct) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CpsReceiptSplitDtl.class);
        detachedCriteria.add(Restrictions.eq("mainGopayOrderId", mainGopayOrderId));
        detachedCriteria.add(Restrictions.eq("splitAcct", splitAcct));

        List<CpsReceiptSplitDtl> cpsReceiptSplitDtlList = findByCriteria(detachedCriteria);
        if(!cpsReceiptSplitDtlList.isEmpty()){
            return cpsReceiptSplitDtlList.get(0);
        }
        return null;
    }

}
